import request from '@/utils/request'
import defaultSettings from '@/settings'
const Base64 = require('js-base64').Base64
const qs = require('qs')
import {
  post,
  postObj
} from '@/utils/api.js'

export const baseUrlConfig = {
  // 此接口只返回文件ID
  uploadUrl: `${defaultSettings.ctx.authorityApp}/userFile/upload`,
  // 与下面的服务相同但是路径不同 主要为USER服务
  uploadUrlForUser: `${defaultSettings.ctx.authorityApp}/s/upload`,
  // 与下面的服务相同但是路径不同 主要为Apchain服务
  uploadUrlForApchain: `${defaultSettings.ctx.apchainApp}/s/upload`,
  // 参数ID 和 serviceName
  downloadFile: `${defaultSettings.ctx.authorityApp}/s/download`,
  // 此接口返回文件信息
  uploadUrlNew: `${defaultSettings.ctx.uploadApp}/s/upload`,
  findFileMetasByFileIds: `${defaultSettings.ctx.uploadApp}/s/findFileMetasByFileIds`,
  findFileByFileId: `${defaultSettings.ctx.uploadApp}/s/findFileByFileId`,
  // fileContentList: `${defaultSettings.ctx.uploadApp}/s/findFileContentByFileId`,
}

//登录页面获取租户列表
export function tenantInfoList() {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/pre/findTenantList`,
    method: 'get'
  })
}

// export function getDownloadServiceName(params) {
//   return request({
//     url: `${defaultSettings.ctx.authorityApp}/s/common/getServiceName`,
//     method: 'get',
//   })
// }

//后台用户登录
export function login(tenantCode,userName,passwd,verifyCode,key) {
  let data = {
    tenantCode,
    userName,
    passwd,
    verifyCode,
    key,
  };
  if(!verifyCode) {
    delete data.verifyCode
  }
  let lang = localStorage.getItem('lang') || 'zh'
  return request({
    url: `${defaultSettings.ctx.authorityApp}/pre/vueLogin?lang=${lang}`,
    method: 'post',
    data
  })
}

//退出登录
export function logout(token = '') {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/pre/vueLogout`,
    method: 'get'
  })
}

//获取验证码
export function getCodeImg() {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/captcha/vueImage`,
    method: 'get'
  })
}

//获取用户信息
export function getInfo() {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/getUserInfo`,
    method: 'get'
  })
}

//设置密码
export function setPwd(data) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/afterLogin/setPwd`,
    method: 'post',
    data
  })
}

//设置密码
export function resetPwd(data) {
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/afterLogin/resetPwd`,
    method: 'post',
    data,
  })
}

//权限检查
export function checkPermissions(permissions) {
  //const p = qs.stringify({permissions})
  return request({
    url: `${defaultSettings.ctx.authorityApp}/s/checkPermissions?permissions=${permissions?permissions.join(','):''}`,
    method: 'get'
  })
}

export function property() {

  return request({
    url: `${defaultSettings.ctx.authorityApp}/pre/property`,
    method: 'get'
  })
}

export function uploadUrl() {

  return `${defaultSettings.ctx.authorityApp == '' ? 'authority-app' : defaultSettings.ctx.authorityApp}/userFile/upload`;
}

export function delReq(url, data) {
  return request({
    url: `${url}`,
    method: 'post',
    data: data,
  })
}

export function listReq(url, queryData, orderByColumn = '',isAsc = 'desc') {
  if (queryData) {
    var {
      current,
      limit
    } = _.clone(queryData);
    queryData['current'] = undefined;
    queryData['limit'] = undefined;
  }
  return request({
    url: `${url}`,
    method: 'post',
    data: queryData ? {
      param: queryData,
      orderBy: {
        orderByColumn,
        isAsc
      },
      current,
      limit
    } : undefined,
  })
}
// 不包裹的请求
export const listReqNoBody = (url, data) => post(`${url}`, data);
// 批次弹窗内保存按钮
export function listReqFormData(url, queryData) {
  return request({
    url: `${url}`,
    method: 'post',
    data: queryData,
    transformRequest: [
      function (data) {
        let ret = '';
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret;
      }
    ]
  })
}
//导出数据API
export function exportReq(url, queryData, json) {
  return request({
    url: `${url}`,
    headers: {
      'exportType': 'XLSX',
      'HT-super-export-meta': Base64.encode(JSON.stringify(json)),
    },
    method: 'post',
    data: queryData
  })
}
export function importReq(url, data) {
  return request({
    url: `${url}`,
    method: 'post',
    data: data
  })
}

export function getReq(url,params) {
  return request({
    url: `${url}`,
    method: 'get',
    params
  })
}

export function postReq(url,params) {
  return request({
    url: `${url}`,
    method: 'post',
    params
  })
}


export function downloadfile(url,fileId){
  return request({
    url:`${url}`,
    method: 'post',
    params:{fileId:fileId},
    responseType: 'blob'
  })
}
